package com.appbee.songs.client.ui;

import java.util.List;

import javax.inject.Inject;

import com.appbee.core.client.jsni.JsArrayConverter;
import com.appbee.gdata.youtube.client.cell.RemoveSimpleEntryListener;
import com.appbee.gdata.youtube.client.cell.SimpleEntryRemoveCell;
import com.appbee.gdata.youtube.client.design.cells.YoutubeCellListResources;
import com.appbee.gdata.youtube.client.model.SimpleEntry;
import com.appbee.gdata.youtube.client.search.Entry;
import com.appbee.gdata.youtube.client.search.MediaContent;
import com.appbee.gdata.youtube.client.search.MediaGroup;
import com.appbee.gdata.youtube.client.search.MediaThumbnail;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.editor.client.EditorDelegate;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.editor.client.ValueAwareEditor;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SingleSelectionModel;

public class EntryEditor extends Composite implements ValueAwareEditor<String>,LeafValueEditor<String>, RemoveSimpleEntryListener
{

	@UiField(provided=true)
	CellList<SimpleEntry> list;
	SingleSelectionModel<SimpleEntry> selectionModel = new SingleSelectionModel<SimpleEntry>();
	private JsArray<SimpleEntry> entryArray;

	@Inject
	public EntryEditor(YoutubeCellListResources res,SimpleEntryRemoveCell cell)
	{
		list = new CellList<SimpleEntry>(cell,res);
		list.setSelectionModel(selectionModel);
		list.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);
		cell.setListener(this);
		initWidget(list);
	}

	@Override
	public void setDelegate(EditorDelegate<String> delegate)
	{
	}

	@Override
	public void flush()
	{
		
	}

	@Override
	public void onPropertyChange(String... paths)
	{
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setValue(String value)
	{
		GWT.log("setValue: "+value);
		if (value == null)
			this.entryArray = (JsArray<SimpleEntry>) JsArray.createArray();
		else this.entryArray = SimpleEntry.parseJson("v="+value);
		this.list.setRowData(JsArrayConverter.toList(entryArray));
	}
	
	
	public void add(Entry entry)
	{
		SimpleEntry e = SimpleEntry.create2(entry.getId(),entry.getTitle(),"");
		MediaGroup mediaGroup = entry.getMediaGroup();
		if(mediaGroup!=null && mediaGroup.getMediaThumbnail()!=null && mediaGroup.getMediaThumbnail().length()>0)
		{
			MediaThumbnail mediaThumbnail = mediaGroup.getMediaThumbnail().get(0);
			e.setThumbnail(mediaThumbnail.getUrl());
			JsArray<MediaContent> mediaContent = mediaGroup.getMediaContent();
			if(mediaContent.length()>0)
			{
				e.setDuration(mediaContent.get(0).getDuration());
			}
		}
		
		entryArray.push(e);
		List<SimpleEntry> list2 = JsArrayConverter.toList(entryArray);
		this.list.setRowData(list2);
	}
	
	public void clear()
	{
		entryArray = JsArrayConverter.create();
	}
	
	@Override
	public String getValue()
	{
		StringBuffer value = new StringBuffer();
		value.append("[");
		int length = entryArray.length();
		for(int i=0;i<length;i++)
		{
			SimpleEntry entry = entryArray.get(i);
			if(i>0) value.append(",");
			value.append(new JSONObject(entry).toString());
		}
		value.append("]");
		
		GWT.log("getValue: "+value.toString());
		return value.toString();
	}

	@Override
	public void onRemove(SimpleEntry entry)
	{
		for (int i = 0; i < entryArray.length(); i++)
		{
			if(entry.getId().equals(entryArray.get(i).getId()))
			{
				JsArrayConverter.remove(entryArray, i);
				break;
			}
		}
		List<SimpleEntry> list2 = JsArrayConverter.toList(entryArray);
		this.list.setRowData(list2);
	}
	
	public void addSelectionListener(Handler handler)
	{
		selectionModel.addSelectionChangeHandler(handler);
	}
	
	public SimpleEntry getSelectedEntry()
	{
		return selectionModel.getSelectedObject();
	}

	public void selectFirstEntry()
	{
		if(entryArray.length()>0)
			selectionModel.setSelected(entryArray.get(0), true);
	}

}
